#!/usr/bin/python
# -*- coding:UTF8 -*-


from config import CrawlerData
import pandas as pd
import requests
import json
import sys

print("""
    1-纪录片；2-传记；3-犯罪；4-历史；5-动作；
    6-情色；7-歌舞；8-儿童；10-悬疑；11-剧情；
    12-灾难；13-爱情；14-音乐；15-冒险；16-奇幻；
    17-科幻；18-运动；19-惊悚；20-恐怖；22-战争；
    23-短篇；24-喜剧；25-动画；26-同性；27-西部；
    28-家庭；29-武侠；30-古装；31-黑色电影
""")
type = input("请输入想看的电影类型:")
limit = input("您想查看排名前多少位的电影:")
sys.stdout = CrawlerData.sys_stdout
url = 'https://movie.douban.com/j/chart/top_list'
user_agent = CrawlerData.user_agent_edge
params = {
    'type': type,
    'interval_id': '100:90',
    'action': '',
    'start': 0,
    'limit': limit
}
r = requests.get(url=url, headers=user_agent, params=params)
r.encoding = 'utf8'
dicts = json.loads(r.text)
rank = [dicts['rank'] for dicts in dicts]
score = [dicts['score'] for dicts in dicts]
title = [dicts['title'] for dicts in dicts]
actors = [dicts['actors'] for dicts in dicts]
regions = [dicts['regions'] for dicts in dicts]
release_date = [dicts['release_date'] for dicts in dicts]
url = [dicts['url'] for dicts in dicts]
data = pd.DataFrame({
    '排名': rank,
    '评分': score,
    '电影名称': title,
    '主演': actors,
    '国家或地区': regions,
    '上映时间': release_date,
    '网址': url
})
data.index = data.index + 1
data.to_excel('./测试数据/movies.xlsx')
